package com.sgb.kjwl.view.ui.activity.transport.driver.goods;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.PayInfoEntity;
import com.sgb.kjwl.model.entity.self.GoodsSourceDetail;
import com.sgb.kjwl.model.entity.self.UserAccount;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.view.entity.PermissionEntity;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author : 熊丽
 * @date :2021/1/9 0009$ 12:02$
 * @Description :竞价/货源详情$
 */
public class GoodsDetailModel extends AndroidViewModel {
      public MutableLiveData<GoodsSourceDetail> GoodsSourceDetailData = new MutableLiveData<>();
      public MutableLiveData<String> GoodsSourceDetailDataEmpty = new MutableLiveData<>();
      public MutableLiveData upAndDownGoods = new MutableLiveData();
      public MutableLiveData deleteInfoSuccess = new MutableLiveData();
      public MutableLiveData<UserAccount> confirmFundAccountData = new MutableLiveData<>();
    public MutableLiveData<PermissionEntity> setUserInfo= new MutableLiveData<>();
    public MutableLiveData<Response<BaseEntity>> checkInfo = new MutableLiveData<>();
    public MutableLiveData<PayInfoEntity> ldUnfreeze = new MutableLiveData<>();

    public GoodsDetailModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 普通货源详情
     */
    public void GeneralPrjDetailModel(Context context,String sourceNo,Integer source) {
        DialogHelper.showProgressDialog(context, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        String isLogin = UserOperating.getInstance().isLogged() ? "true" : "false";
        NewAPIManager.getInstance().getLogisticsAPI().getGoodsSourceDetail(isLogin, sourceNo,source).enqueue(new ICallBack<BaseEntity<GoodsSourceDetail>>() {
            @Override
            public void onSuccess(BaseEntity<GoodsSourceDetail> response) {
                DialogHelper.dismissProgressDialog();
                if (response.getData() == null){
                    MToast.showLongToast(context,response.getMsg());
                    return;
                }
                if (response.getState().equals("ok")) {
                    GoodsSourceDetailData.postValue(response.getData());
                } else if ("201".equals(response.getCode()) && response.getState().equals("fail")) {
                    GoodsSourceDetailDataEmpty.postValue(response.getMsg());
                } else if (response.getState().equals("fail")) {
                    MToast.showToast(context, response.getMsg());
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<GoodsSourceDetail>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, "操作失败");
            }
        });
    }

    /**
     * 上架
     */
    public void downAndUpGoods(Context context, String no) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().upAndDownGoods(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity response) {
                DialogHelper.dismissProgressDialog();
                if (response == null)
                    return;
                if (response.getCode().equals("200")) {
                    upAndDownGoods.postValue(response.getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }

    /**
     * 删除
     */
    public void deleteGoods(Context context,String no) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().deleteGoods(String.valueOf(no)).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null)
                    return;
                if (response.getCode().equals("200")) {
                    deleteInfoSuccess.postValue(response.getMsg());
                } else {
                    MToast.showToast(context, "删除失败");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }

    public void confirmFundAccount(Context context,String no) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, Object> map = new HashMap<>();
        map.put("goodsSourceNo", no);
        APIManager.getInstance().getDriverAPI().confirmFundAccount(map).enqueue(new ICallBack<BaseEntity<UserAccount>>() {
            @Override
            public void onSuccess(BaseEntity<UserAccount> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.getState() == null) {
                    return;
                }
                if (response.getState().equals(APIManager.RESPONSE_OK)) {
                    confirmFundAccountData.postValue(response.getData());


                } else {
                    MToast.showToast(context, TextUtils.isEmpty(response.getMsg()) ? response.getState() : response.getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserAccount>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }

    public void refresh(Context context) {
        //获取用户数据
        //TODO 接口有时候报错，改为不提示报错内容 Callback
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().getLogisticsPermissions().enqueue(new ICallBack<BaseEntity<PermissionEntity>>() {
            @Override
            public void onSuccess(BaseEntity<PermissionEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null) {
                    return;
                }
                if (response.getData() == null) return;
                if (response.getState().equals("ok")) {
                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, response.getData().getUserType());
                    UserOperating.getInstance().setUserInfo(response.getData());
                    setUserInfo.postValue(response.getData());
                }else {
                    setUserInfo.postValue(response.getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PermissionEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    /**
     * 取消支付立即解冻当前货源
     * @param goodsSourceNo
     * @param orderNo
     */
    public void unfreeze(Context activity,String goodsSourceNo, String orderNo) {
        //showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", orderNo);
        map.put("goodsSourceNo", goodsSourceNo);
        NewAPIManager.getInstance().getLogisticsAPI().unfreeze(map).enqueue(new ICallBack<BaseEntity<PayInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<PayInfoEntity> response) {
                // showContentView();
                if (response==null)return;
                if (response.getState().equals("ok")) {
                    ldUnfreeze.postValue(response.getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PayInfoEntity>> call, Throwable t) {
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
}
